package com.zfy.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfy.cmsservice.entity.Teachers;
import com.zfy.cmsservice.entity.query.TeacherQuery;
import com.zfy.cmsservice.mapper.TeachersMapper;
import com.zfy.cmsservice.service.TeachersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zfy.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 教师表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-11-05
 */
@Service
public class TeachersServiceImpl extends ServiceImpl<TeachersMapper, Teachers> implements TeachersService {
    @Autowired
    private  TeachersMapper teachersMapper;
    @Override
    public R pageTeacherCondition(long page, long size, TeacherQuery teacherQuery) {
        // 创建分页page对象
        Page<Teachers> pageTeacher = new Page<>(page, size);
        // 构建条件Wrapper
        QueryWrapper<Teachers> wrapper = new QueryWrapper<>();
        // 从query中获取条件
        if (teacherQuery != null) {
            String name = teacherQuery.getName();
            Integer gender = teacherQuery.getGender();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
            // 构造调节
            if (!StringUtils.isEmpty(name)) {
                // 构建条件
                wrapper.like("name", name);
            }
            if (gender != null) {
                wrapper.eq("gender", gender);
            }
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("gmt_create", begin); // ge >=
            }
            if (!StringUtils.isEmpty(end)) {
                wrapper.le("gmt_create", end);
            }
        }
        // 排序
        wrapper.orderByDesc("gmt_modified");
        // 调用方法实现带分页条件查询
        this.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal(); // 总记录数
        List<Teachers> records = pageTeacher.getRecords(); // 数据 list 集合
        return R.ok().data("total", total).data("content", records);
    }

    @Override
    public List<Teachers> findAllTeacher() {
        return teachersMapper.findAllTeacher();
    }
}
